<template>
  <div class="carousel-container">
    <div class="carousel-slide" :style="{ transform: `translateX(-${currentSlide * 100}%)` }">
      <div v-for="(image, index) in images" :key="index" class="carousel-image-container">
        <img :src="image.url" :alt="image.title" class="carousel-image">
        <div class="carousel-caption">{{ image.title }}</div>
      </div>
    </div>
    <button class="carousel-control prev" @click="prevSlide">
      <i class="fas fa-chevron-left"></i>
    </button>
    <button class="carousel-control next" @click="nextSlide">
      <i class="fas fa-chevron-right"></i>
    </button>
    <div class="carousel-indicators">
      <div v-for="(_, index) in images" 
           :key="index" 
           :class="['indicator', { active: currentSlide === index }]"
           @click="setSlide(index)"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Carousel',
  props: {
    images: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      currentSlide: 0,
      carouselInterval: null
    }
  },
  methods: {
    nextSlide() {
      this.currentSlide = (this.currentSlide + 1) % this.images.length
    },
    prevSlide() {
      this.currentSlide = (this.currentSlide - 1 + this.images.length) % this.images.length
    },
    setSlide(index) {
      this.currentSlide = index
    },
    startCarousel() {
      this.carouselInterval = setInterval(() => {
        this.nextSlide()
      }, 5000)
    },
    stopCarousel() {
      clearInterval(this.carouselInterval)
    }
  },
  mounted() {
    this.startCarousel()
  },
  beforeUnmount() {
    this.stopCarousel()
  }
}
</script>

<style scoped>
.carousel-container {
  margin: 30px auto;
  position: relative;
  overflow: hidden;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  height: 500px;
}

.carousel-slide {
  display: flex;
  width: 100%;
  height: 100%;
  transition: transform 0.5s ease-in-out;
}

.carousel-image-container {
  position: relative;
  min-width: 100%;
  height: 100%;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-caption {
  position: absolute;
  bottom: 30px;
  left: 30px;
  color: white;
  font-size: 2rem;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  padding: 15px;
  cursor: pointer;
  font-size: 1.5rem;
  z-index: 10;
  transition: background 0.3s;
}

.carousel-control:hover {
  background: rgba(0, 0, 0, 0.8);
}

.carousel-control.prev {
  left: 0;
  border-radius: 0 5px 5px 0;
}

.carousel-control.next {
  right: 0;
  border-radius: 5px 0 0 5px;
}

.carousel-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
}

.indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: background 0.3s;
}

.indicator.active {
  background: white;
}

@media (max-width: 768px) {
  .carousel-container {
    height: 300px;
  }
  
  .carousel-caption {
    font-size: 1.5rem;
    bottom: 20px;
    left: 20px;
  }
}
</style>